<template>
  <el-dialog
    :title="!dataForm.developerId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="开发商名字" prop="developerName">
      <el-input v-model="dataForm.developerName" placeholder="开发商名字"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="developerSort">
      <el-input v-model="dataForm.developerSort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="开发商状态 0.正常 1.禁用" prop="developerStatus">
      <el-input v-model="dataForm.developerStatus" placeholder="开发商状态 0.正常 1.禁用"></el-input>
    </el-form-item>
    <el-form-item label="地区ID" prop="areaId">
      <el-input v-model="dataForm.areaId" placeholder="地区ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          developerId: 0,
          developerName: '',
          developerSort: '',
          developerStatus: '',
          areaId: ''
        },
        dataRule: {
          developerName: [
            { required: true, message: '开发商名字不能为空', trigger: 'blur' }
          ],
          developerSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          developerStatus: [
            { required: true, message: '开发商状态 0.正常 1.禁用不能为空', trigger: 'blur' }
          ],
          areaId: [
            { required: true, message: '地区ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.developerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.developerId) {
            this.$http({
              url: this.$http.adornUrl(`/building/developer/info/${this.dataForm.developerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.developerName = data.developer.developerName
                this.dataForm.developerSort = data.developer.developerSort
                this.dataForm.developerStatus = data.developer.developerStatus
                this.dataForm.areaId = data.developer.areaId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/building/developer/${!this.dataForm.developerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'developerId': this.dataForm.developerId || undefined,
                'developerName': this.dataForm.developerName,
                'developerSort': this.dataForm.developerSort,
                'developerStatus': this.dataForm.developerStatus,
                'areaId': this.dataForm.areaId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
